package com.arpa.oms.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 数据脱敏工具类
 */
public class DataMaskingUtils {
    /**
     * 手机号码前三后四脱敏
     */
    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isBlank(mobile) || (mobile.length() != 11)) {
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1****$2");
    }

    /**
     * 姓名脱敏
     */
    public static String nameEncrypt(String name) {
        if (StringUtils.isBlank(name) || (name.length() == 1)) {
            return name;
        }
        char[] chars = name.toCharArray();
        if (chars.length == 2) {
            name = name.replaceFirst(name.substring(1), "*");
        } else if (chars.length > 2) {
            StringBuffer s = new StringBuffer();
            for (int i=0; i< chars.length - 2; i++) {
                s.append("*");
            }
            name = name.replaceAll(name.substring(1, chars.length-1), s.toString());
        }
        return name;
    }

    /**
     * 地址脱敏
     */
    public static String addressEncrypt(String address) {
        if (StringUtils.isBlank(address) || (address.length() == 1)) {
            return address;
        }
        char[] chars = address.toCharArray();
        if (chars.length == 2) {
            address = address.replaceFirst(address.substring(0, 1), "*");
        } else if (chars.length == 3) {
            address = address.replaceAll(address.substring(0, chars.length-1), "**");
        } else {
            address = address.replaceAll(address.substring(chars.length-4, chars.length-1), "***");
        }
        return address;
    }
}
